-- Copyright 2010 Benjamin Leppard

-- This file is part of Picasa Metadata Import Into Lightroom.

-- Picasa Metadata Import Into Lightroom is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.

-- Picasa Metadata Import Into Lightroom is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.

-- You should have received a copy of the GNU General Public License
-- along with Picasa Metadata Import Into Lightroom.  If not, see <http://www.gnu.org/licenses/>.

local LrPathUtils = import 'LrPathUtils'
local LrFileUtils = import 'LrFileUtils'
local LrApplication = import 'LrApplication'
local LrTasks = import 'LrTasks'
local LrProgressScope = import 'LrProgressScope'

require "Logger"
require "Global"

-- local Require = require 'Require'.path ("../../../progs/debugscript.lrdevplugin")
-- local Debug = require 'Debug'.init ()
pluginRequire 'common/libraries/strict.lua'

local PicasaMetadata = pluginRequire "common/PicasaMetadata.lua"
-- local Profiler = pluginRequire       "common/Profiler.lua"
local TablePrint = pluginRequire     "common/libraries/table_print.lua"
-- local Profiler = pluginRequire       "common/Profiler.lua"
local toString = TablePrint.to_string


local catalog = LrApplication.activeCatalog()

function matchesPattern(str, pattern)
	return "" == str:gsub(pattern, "")
end

function handleCropProperty(value)
-- Currently the Lightroom SDK doesn't allow cropping an image.

--	Logger:log("handleCropProperty: value: " .. value)
--	local tl, tr, bl, br = value:match('%s*rect%(([0-9]-)%s*([0-9]-)%s*([0-9]-)%s*([0-9]-)%)%s*')
--	Logger:log("handleCropProperty: tl: " .. tl ..", tr: " .. tr ..", bl: " .. bl ..", br: " .. br)
end

function manualUpdate(context)
	
	local mainFunc = function( p1 )

		Logger:log("ManualUpdate: Update Started")
		local selectedPhotos = catalog:getTargetPhotos() 
		local photoProperties = PicasaMetadata.getPropertiesFromPhotos(context, selectedPhotos)
		local picasaIniFileNameToPhotoPathToPropertyToValue = photoProperties.PicasaIniFileNameToPhotoPathToPropertyToValue
		local photoPathToPhoto = photoProperties.PhotoPathToPhoto
		
--		Logger:log("photoProperties: " .. toString(photoProperties))
		
		PicasaMetadata.setProperties(context, picasaIniFileNameToPhotoPathToPropertyToValue, photoPathToPhoto)
		Logger:log("ManualUpdate: Completed Update")
	end
	
	Logger:log("ManualUpdate: Running Manual Update")
	--LrTasks.startAsyncTask( Debug.showErrors(mainFunc), "manualUpdate" )
	LrTasks.startAsyncTask(mainFunc, "manualUpdate" )
	
	
	-- If we want error handling use LrFunctionContext
	-- if sts then
		-- Logger:log("ManualUpdate: Completed Update")
	-- else
		-- Logger:log("ManualUpdate: Failed to update: " .. other)
		-- -- rc-utils.defaultFailureHandler( false, other )
	-- end
end

manualUpdate()